package co.edu.uniandes.entregAndes.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import co.edu.uniandes.entregAndes.fachada.EntregAndes;
import co.edu.uniandes.entregAndes.vos.EnviosValue;

/**
 * Servlet implementation class ServletUsuario
 */
public class ServletUsuario extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private static EntregAndes entregAndes = EntregAndes.darInstancia();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ServletUsuario() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String requerimiento = request.getParameter("requerimiento");

		if (requerimiento.equals("consultar")) {
			try {
				ArrayList<EnviosValue> resp = entregAndes
						.respConsultaEnviosValueMetRF2(
								request.getParameter("emailUsuario"),
								request.getParameter("tipoCriterio"),
								request.getParameter("valorCriterio"),
								request.getParameter("orden"));

				PrintWriter pw = response.getWriter();
				for (EnviosValue ev : resp) {
					pw.println("<b>" + ev.darId() + "</b><br>");
					pw.println("-- Estado:" + ev.darEstado() + "<br>");
					pw.println("-- Oficina Destino:" + ev.darCiudadDestino()
							+ "<br><br>");
				}
			} catch (Exception e) {
				response.getWriter().println(e);
			}
		} else if (requerimiento.equals("consultar_vigentes")) {
			try {
				if (request.getParameter("optionsRadios").equals("corresponde")) {
					String p = request.getParameter("valor_criterio");
					ArrayList<EnviosValue> res = entregAndes
							.respConsultaEnviosValueMetRF41(
									request.getParameter("f_inicial"),
									request.getParameter("f_final"),
									request.getParameter("tipoCriterio"),
									request.getParameter("valor_criterio"),
									request.getParameter("usuario_origen"));
					PrintWriter pw = response.getWriter();
					for (EnviosValue ev : res) {
						pw.println("<b>" + ev.darId() + "</b><br>");
						pw.println("-- Estado:" + ev.darEstado() + "<br>");
						pw.println("-- Oficina Destino:" + ev.darCiudadDestino()
								+ "<br><br>");
					}

				} else {
					ArrayList<EnviosValue> res = entregAndes
							.respConsultaEnviosValueMetRF42(
									request.getParameter("f_inicial"),
									request.getParameter("f_final"),
									request.getParameter("tipoCriterio"),
									request.getParameter("valor_criterio"),
									request.getParameter("usuario_origen"));
					PrintWriter pw = response.getWriter();
					for (EnviosValue ev : res) {
						pw.println("<b>" + ev.darId() + "</b><br>");
						pw.println("-- Estado:" + ev.darEstado() + "<br>");
						pw.println("-- Destino:" + ev.darCiudadDestino()
								+ "<br><br>");
					}

				}

			} catch (Exception e) {
				response.getWriter()
						.println(
								"Serious error.\n All shortcuts have disappeared.\n Screen. Mind. Both are blank.<br><br>"
										+ e.getMessage());
			}
		} else {
			response.getWriter()
					.println(
							"Serious error.\n All shortcuts have disappeared.\n Screen. Mind. Both are blank.");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
}
